//
//  XYPresentationManager.swift
//  SinaWeibo
//
//  Created by 韩啸宇 on 15/12/25.
//  Copyright © 2015年 backy. All rights reserved.
//

import UIKit

class XYPresentationManager: NSObject, UIViewControllerTransitioningDelegate {

    /// 记录控制器是出现还是消失
    private var isPresent = false

    /// 弹出菜单尺寸
    var presentSize = CGSizeZero

    // MARK: - 自定义转场动画
    /**
     返回一个控制弹出控制器的对象
     
     :param: presented  被展示的控制器
     :param: presenting 发起弹出的控制器
     
     :returns: 用于控制器弹出控制器的对象
     */
    func presentationControllerForPresentedViewController(presented: UIViewController, presentingViewController presenting: UIViewController, sourceViewController source: UIViewController) -> UIPresentationController? {
        
        let pc = XYPresentationController(presentedViewController: presented, presentingViewController: presenting)
        pc.presentSize = presentSize
        return pc
    }
    
    /**
     控制器如何弹出
     */
    func animationControllerForPresentedController(presented: UIViewController, presentingController presenting: UIViewController, sourceController source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresent = true
        return self
        
    }
    
    /**
     控制器如何消失
     */
    func animationControllerForDismissedController(dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        
        isPresent = false
        return self
        
    }
}

// MARK: - 自定义转场显示代理方法
extension XYPresentationManager: UIViewControllerAnimatedTransitioning {
    /**
     动画显示时长
     */
    func transitionDuration(transitionContext: UIViewControllerContextTransitioning?) -> NSTimeInterval {
        
        return 998
    }
    
    /**
     控制弹出的控制器如何显示和消失
     无论展示或消失都会调用该方法, 实现该方法后modal控制器视图如何添加, 如何动画系统都不会帮我们做了
     
     :param: transitionContext 上下文中包含了需要的所有参数
     */
    func animateTransition(transitionContext: UIViewControllerContextTransitioning) {
        
        isPresent ? willPresentedController(transitionContext) : willDismissedController(transitionContext)
    }
    
    /**
     如何出现
     
     :param: transitionContext 转场上下文
     */
    func willPresentedController(transitionContext: UIViewControllerContextTransitioning) {
        
        // 1.拿到被展现的视图
        guard let toView = transitionContext.viewForKey(UITransitionContextToViewKey) else {
            XYLog("addSubview Error")
            return
        }
        
        // 2.手动添加菜单到容器视图
        transitionContext.containerView()?.addSubview(toView)
        
        // 3.执行动画
        toView.transform = CGAffineTransformMakeScale(1.0, 0.0)
        toView.layer.anchorPoint = CGPoint(x: 0.5, y: 0)
        UIView.animateWithDuration(0.5, animations: { () -> Void in
            
            toView.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                transitionContext.completeTransition(true)
        }
    }
    
    /**
     如何消失
     */
    func willDismissedController(transitionContext: UIViewControllerContextTransitioning) {
        
        guard let fromView = transitionContext.viewForKey(UITransitionContextFromViewKey) else {
            XYLog("removeSubview Error")
            return
        }
        
        UIView.animateWithDuration(0.5, animations: { () -> Void in
            fromView.transform = CGAffineTransformMakeScale(1.0, 0.01)
            }) { (_) -> Void in
                
                transitionContext.completeTransition(true)
                
        }
        
    }
}